using System;
using System.Data.SqlTypes;
using System.Data;
using Jalal.Library.DataStructure;
using Jalal.Library.Utility;
using System.Collections.Generic;
using Jalal.Library.Data;
using BCGC.DAL;

namespace BCGC.BLL.BusinessObject.Resource
{
    public class CheatCode : ResourceBase
    {
        private int cheatCodeId;
        public int CheatCodeId
        {
            get { return cheatCodeId; }
            private set { cheatCodeId = value; }
        }

        private String cheatCodeDetail;
        public String CheatCodeDetail
        {
            get { return cheatCodeDetail; }
            set { cheatCodeDetail = value; }
        }

        public CheatCode()
        {
            this.cheatCodeId = 0;
            this.cheatCodeDetail = string.Empty;
            this.createDateTime = SqlDateTime.MinValue.Value;
            this.description = string.Empty;
            this.relatedGame = null;
            this.title = string.Empty;
            this.isActive = false;
            this.totalView = 0;
            this.rating = 0;
            this.totalRatingVote = 0;
        }

        public CheatCode(DataRow dataRow, bool fullLoad)
        {
            if (dataRow != null)
            {
                if (dataRow.Table.Columns.Contains("CheatCodeId"))
                    this.cheatCodeId = (int)CommonUtility.FilterNull(dataRow["CheatCodeId"], typeof(int));
                if (dataRow.Table.Columns.Contains("CheatCodeDetail"))
                    this.cheatCodeDetail = (string)CommonUtility.FilterNull(dataRow["CheatCodeDetail"], typeof(string));
                if (dataRow.Table.Columns.Contains("CreateDateTime"))
                    this.createDateTime = (DateTime)CommonUtility.FilterNull(dataRow["CreateDateTime"], typeof(DateTime));
                if (dataRow.Table.Columns.Contains("Description"))
                    this.description = (string)CommonUtility.FilterNull(dataRow["Description"], typeof(string));
                if (dataRow.Table.Columns.Contains("Title"))
                    this.title = (string)CommonUtility.FilterNull(dataRow["Title"], typeof(string));
                if (dataRow.Table.Columns.Contains("IsActive"))
                    this.isActive = (bool)CommonUtility.FilterNull(dataRow["IsActive"], typeof(bool));
                if (dataRow.Table.Columns.Contains("TotalView"))
                    this.totalView = (int)CommonUtility.FilterNull(dataRow["TotalView"], typeof(int));
                if (dataRow.Table.Columns.Contains("Rating"))
                    this.rating = (int)CommonUtility.FilterNull(dataRow["Rating"], typeof(int));
                if (dataRow.Table.Columns.Contains("TotalRatingVote"))
                    this.totalRatingVote = (int)CommonUtility.FilterNull(dataRow["TotalRatingVote"], typeof(int));

                if (fullLoad)
                    this.relatedGame = new Game(dataRow);
                else
                    this.relatedGame = null;
            }
            else
            {
                this.cheatCodeId = 0;
                this.cheatCodeDetail = string.Empty;
                this.createDateTime = SqlDateTime.MinValue.Value;
                this.description = string.Empty;
                this.relatedGame = null;
                this.title = string.Empty;
                this.isActive = false;
                this.totalView = 0;
                this.rating = 0;
                this.totalRatingVote = 0;
            }
        }

        private static ReadWriteCustomCollection<CheatCode> LoadFromDataTable(DataTable data)
        {
            ReadWriteCustomCollection<CheatCode> result = new ReadWriteCustomCollection<CheatCode>();

            if (data != null && data.Rows.Count > 0)
            {
                foreach (DataRow row in data.Rows)
                {
                    result.Add(new CheatCode(row, true));
                }
            }
            return result;
        }

        public static void Delete(List<int> cheatCodesToDelete)
        {
            DataFactory.GetDataMiner().BeginTransaction(false);

            try
            {
                int deletedItemCount = ResourceData.DeleteCheatCodes(cheatCodesToDelete);
                if (deletedItemCount == cheatCodesToDelete.Count)
                    DataFactory.GetDataMiner().CommitTransaction();
                else
                    throw new Exception("Failed to delete all items");
            }
            catch (Exception ex)
            {
                DataFactory.GetDataMiner().RollBackTransaction();
                throw ex;
            }
        }

        public static void UpdateActive(List<int> activeCheatCodes, List<int> inactiveCheatCodes)
        {
            DataFactory.GetDataMiner().BeginTransaction(false);

            try
            {
                int updatedItemCount = ResourceData.UpdateActiveCheatCode(activeCheatCodes,
                    inactiveCheatCodes);
                if (updatedItemCount == activeCheatCodes.Count + inactiveCheatCodes.Count)
                    DataFactory.GetDataMiner().CommitTransaction();
                else
                    throw new Exception("Failed to update all items");
            }
            catch (Exception ex)
            {
                DataFactory.GetDataMiner().RollBackTransaction();
                throw ex;
            }
        }

        public static ReadWriteCustomCollection<CheatCode> GetAllCheatCodes(int currentPageIndex, 
            string sortOrder, int pageSize, string title, string gameTitle, DateTime? createDateFrom, 
            DateTime? createDateTo, bool? isActive, out int totalData)
        {
            DataFactory.GetDataMiner().BeginTransaction(true);

            totalData = 0;

            DataTable cheatCodeData = ResourceData.GetAllCheatCodes(currentPageIndex, sortOrder, pageSize, 
                title, gameTitle, createDateFrom, createDateTo, isActive, out totalData);
            ReadWriteCustomCollection<CheatCode> cheatCodes = LoadFromDataTable(cheatCodeData);

            DataFactory.GetDataMiner().CommitTransaction();

            return cheatCodes;
        }

        public static bool Create(string title, int gameId, string description, 
            string cheatCodeDetail, bool isActive)
        {
            DataFactory.GetDataMiner().BeginTransaction(true);
            int id = ResourceData.CreateNewCheatCode(title, gameId, description, cheatCodeDetail, isActive);
            DataFactory.GetDataMiner().CommitTransaction();

            return (id > 0);
        }

        public static CheatCode GetCheatCodeById(int id)
        {
            DataFactory.GetDataMiner().BeginTransaction(true);
            CheatCode cheatCode = null;
            DataTable data = ResourceData.GetCheatCodeById(id);
            if (data != null && data.Rows.Count > 0)
                cheatCode = new CheatCode(data.Rows[0], true);

            DataFactory.GetDataMiner().CommitTransaction();

            return cheatCode;
        }

        public static bool Update(int id, string title, int gameId, string description,
            string cheatCodeDetail, bool isActive)
        {
            DataFactory.GetDataMiner().BeginTransaction(true);
            int updateCount = ResourceData.UpdateCheatCode(id, title, gameId, description,cheatCodeDetail,
                isActive);
            DataFactory.GetDataMiner().CommitTransaction();

            return (updateCount > 0);
        }

        public static ReadWriteCustomCollection<CheatCode> GetCheatCodesByGame(int currentPageIndex, 
            string sortOrder, int pageSize, string title, int gameId, out int totalData)
        {
            DataFactory.GetDataMiner().BeginTransaction(true);

            totalData = 0;

            DataTable cheatCodeData = ResourceData.GetCheatCodesByGame(currentPageIndex, sortOrder, pageSize,
                title, gameId, out totalData);
            ReadWriteCustomCollection<CheatCode> cheatCodes = LoadFromDataTable(cheatCodeData);

            DataFactory.GetDataMiner().CommitTransaction();

            return cheatCodes;
        }
    }

}
